package org.vectrics.recruiter.struts.tag.employee;

import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.DateUtil;
import org.vectrics.common.view.FieldTable;
import org.vectrics.employee.Employee;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.struts.VectricsTagSupport;

/**
 * @author pgoette Tag que muestra los datos del empleado.
 */
public class EmployeeSummaryTag extends VectricsTagSupport {

    private static final long serialVersionUID = 4247431979714424201L;

    /** Id de persona. */
    private String personId;

    /** Id de la compania. */
    private String companyId;

    /*
     * (non-Javadoc)
     * 
     * @see org.vectrics.struts.VectricsTagSupport#makeStartTag()
     */
    @Override
    protected String makeStartTag() {
        String html = "";
        // Recupero el empleado
        Employee employee = RecruitServiceLocator.getCompanyService().findEmployee(new Long(personId),
                new Long(companyId));
        boolean isEmployee = employee != null;
        // Si no es empleado no hago nada
        if (isEmployee) {
            // Genero las tablas
            Table mainTablePrincipal = new Table();
            Row mainRowPrincipal = mainTablePrincipal.addRow();
            Cell mainCellPrincipal = mainRowPrincipal.addCell();
            Table mainTable = new Table();
            mainTable.setWidth(620);
            Row mainRow = mainTable.addRow();
            Cell mainCell = mainRow.addCell();
            // Seteo las caracteristicas de las tablas
            FieldTable fieldTable = new FieldTable();
            fieldTable.setCellPadding(2);
            fieldTable.setWidth("100%");
            fieldTable.setValueCellWidth("65%");
            fieldTable.setValueFontClass("stdFont");

            if (employee.getCurrentStatus() != null) {
                fieldTable.showField(getPhrase("prompt.employee.condition"), employee.getCurrentStatus()
                        .getStatusType().getDescription());
            }

            // Genero una fila con los datos de la function
            if (employee.getFunction() != null) {
                fieldTable.showField(getPhrase("prompt.employee.function"), employee.getFunction().getDescription());
            } else {
                fieldTable.showField(getPhrase("prompt.employee.function"), "-");
            }

            // First working day
            String dateStart = null;
            if (employee.getDateStart() != null) {
                dateStart = DateUtil.getFechaFormateada(employee.getDateStart());
                fieldTable.showField(getPhrase("prompt.employee.date.start"), dateStart);
            } else {
                fieldTable.showField(getPhrase("prompt.employee.date.start"), "-");
            }

            // First employee day
            String dateStartEmployee = null;
            if (employee.getFirstEmployeeDate() != null) {
                dateStartEmployee = DateUtil.getFechaFormateada(employee.getFirstEmployeeDate());
                fieldTable.showField(getPhrase("prompt.employee.firstDay"), dateStartEmployee);
            } else {
                fieldTable.showField(getPhrase("prompt.employee.firstDay"), "-");
            }

            // Last working day
            if (employee.getDateEnd() != null) {
                String dateEnd = DateUtil.getFechaFormateada(employee.getDateEnd());
                fieldTable.showField(getPhrase("prompt.employee.date.end"), dateEnd);
            } else {
                fieldTable.showField(getPhrase("prompt.employee.date.end"), "-");
            }

            fieldTable.showField(getPhrase("prompt.employee.career.rolePosition"), employee.getCareerRolePosition()
                    .toString()
                    + " %");
            fieldTable.showField(getPhrase("prompt.employee.hoursPerDay"), employee.getHoursPerDay().toString());

            if (employee.getLocation() != null) {
                fieldTable.showField(getPhrase("prompt.employee.office.location"), employee.getLocation().getName());
            } else {
                fieldTable.showField(getPhrase("prompt.employee.office.location"), "-");
            }

            if (employee.getMentor() != null) {
                fieldTable.showField(getPhrase("prompt.employee.mentor"), employee.getMentor().getPerson()
                        .getLastName()
                        + ", "
                        + employee.getMentor().getPerson().getFirstName()
                        + " "
                        + employee.getMentor().getPerson().getMiddleName());
            } else {
                fieldTable.showField(getPhrase("prompt.employee.mentor"), "-");
            }

            if (employee.isElegibleMentor()) {
                fieldTable.showField(getPhrase("prompt.employee.elegibleMentor"),
                        getPhrase("prompt.employee.elegibleMentor.true"));
            } else {
                fieldTable.showField(getPhrase("prompt.employee.elegibleMentor"),
                        getPhrase("prompt.employee.elegibleMentor.false"));
            }

            if (employee.getMedicalInsurance() != null) {
                fieldTable.showField(getPhrase("prompt.employee.medical.Insurance"), employee.getMedicalInsurance());
            }

            if (employee.getLastAgreementDay() != null) {
                fieldTable.showField(getPhrase("prompt.employee.last.agreement.day"), DateUtil
                        .getFechaFormateada(employee.getLastAgreementDay()));
            }

            mainCell.addElement(fieldTable);
            mainTable.setPrettyPrint(true);
            mainCellPrincipal.addElement(mainTable);
            // Seteo el html a la variable html
            html = mainTablePrincipal.toString();
        }
        return html;
    }

    /**
     * @return the personId
     */
    public String getPersonId() {
        return personId;
    }

    /**
     * @param personId
     *            the personId to set
     */
    public void setPersonId(final String personId) {
        this.personId = personId;
    }

    /**
     * @return the companyId
     */
    public String getCompanyId() {
        return companyId;
    }

    /**
     * @param companyId
     *            the companyId to set
     */
    public void setCompanyId(final String companyId) {
        this.companyId = companyId;
    }

}
